﻿using Newtonsoft.Json;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using System.Collections;
using System.Globalization;
using System.Windows.Data;

namespace SUNRPA.CONTROLS.IMPL.domain.flowRunningVar {
    /// <summary>
    /// rpa运行中自定义变量类型的基类
    /// </summary>
    public class RPAVariable {
        private string varName;

        public string VarName { 
            get { return varName; }
            set { varName = value; }
        }

        public static Dictionary<VarialbleType, Type> rpaVarTypesDict = new Dictionary<VarialbleType, Type>() {
            { VarialbleType.STRING, typeof(StringModel) },
            { VarialbleType.INT, typeof(IntModel) },
            { VarialbleType.DOUBLE, typeof(DoubleModel) },
            { VarialbleType.BOOL, typeof(BoolModel) },
            { VarialbleType.DATE, typeof(DateModel) },
            { VarialbleType.LIST_SIDB, typeof(ListModel<string>) },
            { VarialbleType.DICTIONARY_SIDB, typeof(Dictionary<string, string>) },
            { VarialbleType.EMAIL, typeof(EmailModel) },
            { VarialbleType.WEBPAGE, typeof(WebPage) },
            { VarialbleType.EXCEL, typeof(ExcelModel) }
        };

        public static RPAVariable CreateVar(string varName, VarialbleType vt, string varValue) {
            JsonSerializerSettings set  = new JsonSerializerSettings();
            set.TypeNameHandling = TypeNameHandling.Auto;
            set.NullValueHandling = NullValueHandling.Ignore;
            switch(vt) {
                case VarialbleType.STRING:
                    return new StringModel(varName, varValue);
                    break;
                case VarialbleType.INT:
                    return new IntModel(varName, int.Parse(varValue));
                    break;
                case VarialbleType.DOUBLE:
                    return new DoubleModel(varName, double.Parse(varValue));;
                    break;
                case VarialbleType.BOOL:
                    return new BoolModel(varName, bool.Parse(varValue));
                    break;
                case VarialbleType.DATE:
                    return new DateModel(varName, DateTime.Parse(varValue));
                    break;
                case VarialbleType.LIST_SIDB:
                    List<string> list = JsonConvert.DeserializeObject<List<string>>(varValue, set);
                    return new ListModel<string>(varName, list);
                case VarialbleType.DICTIONARY_SIDB:
                    Dictionary<string, string> dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(varValue, set);
                    return new DictModel<string, string>(varName, dict);
                case VarialbleType.WEBPAGE:
                    return JsonConvert.DeserializeObject<WebPage>(varValue, set);
                case VarialbleType.EMAIL:
                    return JsonConvert.DeserializeObject<EmailModel>(varValue, set);
                default:
                    return new OtherModel(varName, JsonConvert.DeserializeObject(varValue, set));
                    break;
            }
        }

    }

    public class RPAVariableConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            return (int)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return Enum.Parse(typeof(VarialbleType), value.ToString());
        }
    }
}
